package com.st.springts.controller;


import com.st.springts.demos.web.Result;
import com.st.springts.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @PostMapping("/string")
    public Result setStrinng(@RequestParam String key, @RequestParam String value) {
        redisService.setString(key, value);
        return Result.success("sucess", "成功");
    }

    @GetMapping("/string")
    public Result getStrng(@RequestParam String key) {
        String restring = redisService.getString(key);
        return Result.success("success", restring);
    }

    // List 操作测试
    @PostMapping("/list")
    public Result addToList(@RequestParam String key, @RequestParam String value) {
        redisService.pushToList(key, value);
        return Result.success("sucess", "Added to list");
    }

    @GetMapping("/list")
    public Result getList(@RequestParam String key) {
        return Result.success("sucess", redisService.getList(key));
    }

    // Set 操作测试
    @PostMapping("/set")
    public Result addToSet(@RequestParam String key, @RequestParam String value) {
        redisService.addToSet(key, value);
        return Result.success("sucess", "Added to set");
    }

    @GetMapping("/set")
    public Result getSet(@RequestParam String key) {
        return Result.success("sucess", redisService.getSetMembers(key));
    }

    // ==================== Hash操作接口 ====================

    @PostMapping("/hash")
    public String addHash(@RequestParam String key,
                          @RequestParam String field,
                          @RequestParam String value) {
        redisService.put(key, field, value);
        return "Hash field added";
    }

    @GetMapping("/hash/{key}/{field}")
    public Object getHashField(@PathVariable String key,
                               @PathVariable String field) {
        return redisService.get(key, field);
    }

    @GetMapping("/hash/{key}")
    public Map<Object, Object> getHashAll(@PathVariable String key) {
        return redisService.entries(key);
    }

    @PostMapping("/hash/all")
    public String putAllHash(@RequestParam String key,
                             @RequestBody Map<String, Object> map) {
        redisService.putAll(key, map);
        return "Hash map saved";
    }

    @GetMapping("/hash/all")
    public Map<Object, Object> getAllHash(@RequestParam String key) {
        return redisService.entries(key);
    }

    @GetMapping("/id")
    public Result getDepartMent(@RequestParam Long id) {
        return Result.success("success", "<UNK>");
    }

    // ==================== Sorted Set操作接口 ====================

    @PostMapping("/zset")
    public Boolean addToZSet(@RequestParam String key,
                            @RequestParam String member,
                            @RequestParam double score) {
        Boolean add = redisService.add(key, member, score);
        return add;
    }

    @GetMapping("/zset")
    public Result getToZSet(@RequestParam String key ){
        Long size = redisService.size(key);
      return   Result.success("sucess",size);
    }
}
